package com.yingsheng.bi.hks.template.biz;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.ReqContext;
import com.yingsheng.bi.hks.basic.consts.MsgDict;
import com.yingsheng.bi.hks.basic.exception.ServiceRuntimeException;
import com.yingsheng.bi.hks.basic.utils.HttpUtils;
import com.yingsheng.bi.hks.repository.redis.SubjectRedisService;
import com.yingsheng.bi.hks.template.events.Event;
import com.yingsheng.bi.hks.template.processor.Processor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class PermissionProcessor implements Processor {

    @Autowired
    private SubjectRedisService subjectRedisService;

    @Override
    public void exec(Event event) {
        String requestUri = HttpUtils.getRequestUri();
        String identity = ReqContext.get().getIdentity();
        PUser user = subjectRedisService.getUser(identity);
        String roleId = user.getRoleId();
        List<String> permissions = subjectRedisService.getRolePermission(roleId);
        if (!permissions.contains(requestUri)) {
            throw new ServiceRuntimeException(MsgDict.NO_PERMISSION);
        }

    }

    @Override
    public int getOrder() {
        return 1;
    }
}
